/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "LogFile.h"
#include "core/core.h"

Log* g_pLog = NULL;

Log* coreGetLog() 
{
	return g_pLog;
}

bool coreInitLog(const std::string &filename)
{
	if (!g_pLog) {
		g_pLog = new Log(filename);
		return true;
	}
	return false;
}

Log::Log(const std::string &filename)
:strStream(NULL)
{
	ClearLog(filename);
}

Log::~Log(void)
{
}

void Log::Delete()
{
	DELETE_POINTER(g_pLog);
}

void Log::ClearLog(const std::string &filename)
{
	sFileName = filename;
	STRINGCOUT("Logfile is : " << sFileName.c_str());
	ofstr.open(sFileName.c_str(), std::ios::out);
	ofstr.close();
}

void Log::WriteLog(const std::string &errorReport, const std::string &file, int lineNumber)
{
	ofstr.open(sFileName.c_str(), std::ios::out | std::ios::app);
#ifdef _DEBUG
	if (file != "") {
		ofstr << "--- " << file << ":" << lineNumber << " --\t" << errorReport << std::endl;
	}
	else {
		ofstr << errorReport << std::endl;
	}
#else
		ofstr << "--- " << errorReport << std::endl;
#endif
	if (strStream)
		*strStream << errorReport << std::endl;
	ofstr.close();
}

void Log::WriteError(const std::string &errorReport, const std::string &file, int lineNumber)
{
	ofstr.open(sFileName.c_str(), std::ios::out | std::ios::app);
#ifdef _DEBUG
	if (file != "") {
		ofstr << "-E- " << file << ":" << lineNumber << " --\t" << errorReport << std::endl;
	}
	else {
		ofstr << errorReport << std::endl;
	}
#else
		ofstr << "-E- " << errorReport << std::endl;
#endif
	if (strStream)
		*strStream << errorReport << std::endl;

	ofstr.close();
}

void Log::WriteLuaError(const std::string &errorReport, const std::string &file, int lineNumber)
{
	ofstr.open(sFileName.c_str(), std::ios::out | std::ios::app);
#ifdef _DEBUG
	if (file != "") {
		ofstr << "-L- " << file << ":" << lineNumber << " --\t" << errorReport << std::endl;
	}
	else {
		ofstr << errorReport << std::endl;
	}
#else
		ofstr << "-L- " << errorReport << std::endl;
#endif
	if (strStream)
		*strStream << errorReport << std::endl;
/*
		LPVOID lpMsgBuf;
	FormatMessage(
	FORMAT_MESSAGE_ALLOCATE_BUFFER |
	FORMAT_MESSAGE_FROM_SYSTEM |
	FORMAT_MESSAGE_IGNORE_INSERTS,
	NULL,
	GetLastError(),
	MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
	(LPTSTR) &lpMsgBuf,
	0,
	NULL
	);
	// Display the string.
	//MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
	// Free the buffer.
	LocalFree( lpMsgBuf );
*/
	ofstr.close();
}


void Log::WriteWarning(const std::string &errorReport, const std::string &file, int lineNumber)
{
	ofstr.open(sFileName.c_str(), std::ios::out | std::ios::app);
#ifdef _DEBUG
	if (file != "") {
		ofstr << "-W- "<< file << ":" << lineNumber << " --\t" << errorReport << std::endl;
	}
	else {
		ofstr << errorReport << std::endl;
	}
#else
		ofstr << "-W- " << errorReport << std::endl;
#endif
	if (strStream)
		*strStream << errorReport << std::endl;
	ofstr.close();
}

const std::string& Log::getFilename() const
{
	return sFileName;
}

void Log::setStringStream(std::stringstream *stream)
{
	strStream = stream;
}